package com.jfm.jfmc.core.icc.worker;

import com.jfm.jfmc.core.config.ApplicationContextHelper;
import com.jfm.jfmc.core.constants.TemplateConstants;
import com.jfm.jfmc.core.icc.CollectTaskManage;
import com.jfm.jfmc.core.icc.entity.FrequencyEntity;
import com.jfm.jfmc.core.icc.handle.ResultHandle;
import com.jfm.jfmc.core.icc.policy.ReduceFrequencyPolicy;
import com.jfm.jfmc.core.service.ICollectService;
import com.jfm.jfmm.collector.entity.collect.CollectParam;
import com.jfm.jfmm.collector.entity.collect.CollectResult;
import com.jfm.jfmm.collector.entity.collect.Param;
import com.jfm.jfmm.collector.entity.collect.Result;
import lombok.extern.slf4j.Slf4j;

/**
 * @author DYang
 * @since 2024-10-22 22:10
 *
 * 监控项采集器
 */
@Slf4j
public class CollectWorker implements Runnable {

    private static final ResultHandle resultHandle = ApplicationContextHelper.getBean(ResultHandle.class);

    // 采集任务参数
    private CollectParam collectParam;
    private final ICollectService collectService;

    public CollectWorker(CollectParam collectParam){
        this.collectParam = collectParam;
        this.collectService = ApplicationContextHelper.getBean(ICollectService.class);
    }

    /**
     * 采集降频
     * @param collectParam  入参
     * @param collectResult 采集结果
     */
    private void reduceFrequency(CollectParam collectParam, CollectResult collectResult) {
        FrequencyEntity frequencyEntity = ReduceFrequencyPolicy.reducePolicy(collectResult);
        if (frequencyEntity == null) return;

        // 停止采集
        if (!frequencyEntity.isEnabled()) {
            CollectTaskManage.stopCollect(collectParam.getParam().getMetricId());
            return;
        }
        // 采集频率不一致，重置采集器
        if (frequencyEntity.getFrequency() != collectParam.getParam().getFrequency()) {
            Param param = collectParam.getParam();
            CollectTaskManage.updateCollectFrequency(param.getMetricId(), frequencyEntity.getFrequency());
        }

    }

    @Override
    public void run() {
        try {
            // 先更新本地缓存参数,以管理器中的参数为中，采集过程中采集参数会动态调整
            this.collectParam = CollectTaskManage.getCollectParam(collectParam.getParam().getMetricId());
            assert collectParam != null;
            String collectType = collectParam.getParam().getCollectType();
            CollectResult collectResult = null;
            if (TemplateConstants.COLLECT.equals(collectType)){
                collectResult = collectService.collectOne(collectParam);
            }else {
                collectResult = new CollectResult();
                collectResult.setProtocol(collectParam.getProtocol());
                collectResult.setResult(new Result());
                collectResult.setStartTime(System.currentTimeMillis());
            }

            // 设置数据
            Result result = collectResult.getResult();
            if (result == null) {
                log.error("采集结果为空，采集参数：{},采集数据：{}",collectParam,collectResult);
                return;
            }
            result.setTimeUnit(Param.transformTimeUnit(collectParam.getParam().getTimeUnit()));
            result.setMetricId(collectParam.getParam().getMetricId());
            result.setTimeout(collectParam.getParam().getTimeout());
            result.setFrequency(collectParam.getParam().getFrequency());
            result.setMetricCode(collectParam.getParam().getMetricCode());
            result.setCollectType(collectParam.getParam().getCollectType());
            result.setMetricCode(collectParam.getParam().getMetricCode());
            result.setDeviceCode(collectParam.getParam().getDeviceCode());

            // 处理数据
            resultHandle.handle(collectResult);

            // 采集降频
            this.reduceFrequency(collectParam,collectResult);
        } catch (Exception e) {
            log.error("采集器执行异常", e);
        }
    }
}
